﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using CustomsSystem.BLL;

namespace WEB.Emp
{
    public partial class EmpIndex : System.Web.UI.Page
    {
        public DataSet ds;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //获取岗位编号
                if (Request.QueryString["jobno"] != "")
                {
                    string codeID = Convert.ToString(Request.QueryString["jobno"]);
                    DropDownList1.SelectedValue = codeID;
                }
                //岗位列表获取数据
                JobManage job = new JobManage();
                DropDownList1.DataSource = job.GetAllList();
                DropDownList1.DataTextField = "JobName";
                DropDownList1.DataValueField = "JobNo";
                DropDownList1.DataBind();
            }
            //人员列表获取数据
            EmpManage empMAN = new EmpManage();
            ds = empMAN.GetList(" jobNo='".Trim() + DropDownList1.Text.Trim() + "'".Trim());
            gvEMP.DataSource = ds;
            gvEMP.DataBind();

        }

        #region 转到编辑页面
        protected void gvEMP_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvEMP.SelectedIndex = e.NewEditIndex;
            Session["jobNo"] = DropDownList1.Text;
            string empnum = gvEMP.SelectedRow.Cells[0].Text;
            Response.Redirect("ChanceEmp.aspx?EmpNum=" + empnum);
        }

        #endregion

        #region 删除
        //删除
        protected void gvEMP_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = Convert.ToString(gvEMP.DataKeys[e.RowIndex].Value);
            string name = Convert.ToString(gvEMP.Rows[e.RowIndex].Cells[1].Text);
            EmpManage empMan = new EmpManage();
            string Username = Session["UserName"].ToString();

            if (Username.Trim() != name)
            {
                if (empMan.Delete(id))
                {
                    Response.Write("<Script>alert('删除成功!')</script>");

                    ds = empMan.GetList(" jobNo='".Trim() + DropDownList1.Text.Trim() + "'".Trim());
                    gvEMP.DataSource = ds;
                    gvEMP.DataBind();
                }
                else
                {
                    Response.Write("<Script>alert('删除失败!')</script>");
                    return;
                }
            }
            else
            {
                Response.Write("<Script>alert('删除失败!')</script>");
                return;
            }
            
        }
        protected void gvEMP_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            //如果是绑定数据行         
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[1].Text + "\"吗?')");
                }
            }
        }
        #endregion

        ////查询按钮
        //protected void btnAdd_Click(object sender, EventArgs e)
        //{
        //    EmpManage empMAN = new EmpManage();
        //    ds = empMAN.GetList(" jobNo='".Trim() + DropDownList1.Text.Trim() + "'".Trim());
        //    gvEMP.DataSource = ds;
        //    gvEMP.DataBind();
        //}

        //增加员工页面
        protected void Button1_Click(object sender, EventArgs e)
        {
            string emptype = DropDownList1.SelectedValue;
            Response.Redirect("AddEmp.aspx?emptype="+emptype);
        }

        protected void gvEMP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            if (!IsPostBack)
            {
                gvEMP.PageIndex = e.NewPageIndex;
                //gvCustomerManage.DataSource = dt;
                gvEMP.DataBind();//重新绑定GridView数据的函数
            }
        }

        protected void gvEMP_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int id = e.NewSelectedIndex;
            AuthManage AM = new AuthManage();
            Auth auth = new Auth();
            if (AM.Exists(gvEMP.Rows[e.NewSelectedIndex].Cells[0].Text) == false&&AM.Exists(gvEMP.Rows[e.NewSelectedIndex].Cells[1].Text)==false)
            {
                string addusername = gvEMP.Rows[e.NewSelectedIndex].Cells[1].Text;
                string empnum = gvEMP.Rows[e.NewSelectedIndex].Cells[0].Text;
                auth.UserName = addusername;
                auth.EmpNum = empnum;
                AM.Add(auth);
                Session["empnum"] = empnum;
                Response.Redirect("authManage.aspx?empnum=" + empnum);
            }
            else
                Response.Redirect("authManage.aspx?empnum=" + gvEMP.Rows[e.NewSelectedIndex].Cells[0].Text);


        }
    }
}